package mapper

import (
	"goiris/admin/app/web/vo"
	"goiris/common/model"
	"goiris/common/storage"
)

type AdminUserMapper struct {
}

func (aum *AdminUserMapper) Insert(user *model.AdminUser) error {
	return storage.G_DB.Create(&user).Error
}

func (aum *AdminUserMapper) FindOne(cond *model.AdminUser) (*model.AdminUser, error) {
	var (
		err   error
		resut = model.AdminUser{}
	)
	if err = storage.G_DB.Where(&cond).First(&resut).Error; err != nil {
		return nil, err
	}
	return &resut, nil
}

func (aum *AdminUserMapper) UpdateOne(adminUser *model.AdminUser) error {
	return storage.G_DB.Model(&model.AdminUser{}).Update(&adminUser).Error
}

func (aum *AdminUserMapper) Delete(ids []int64) error {
	return storage.G_DB.Delete(model.AdminUser{}, "id in (?)", ids).Error
}

func (aum *AdminUserMapper) FindList(vo *vo.AdminUserVO) (int64, []*model.AdminUser, error) {
	var (
		err    error
		total  int64
		result = make([]*model.AdminUser, 0)
	)
	if err = storage.G_DB.Model(&model.AdminUser{}).Count(&total).Error; err != nil {
		goto ERR
	}
	if err = storage.G_DB.
		Select("id, username, gender, enable, name, age, phone, email, userface, create_time, update_time, label").
		Offset(vo.Start).Limit(vo.Size).Find(&result).Error; err != nil {
		goto ERR
	}
	return total, result, nil
ERR:
	return 0, nil, err
}
